import java.util.Scanner;

public class TestSystem {
    public static void main(String[] args) {
        ScoreSystem system = new ScoreSystem();
        Scanner scanner = new Scanner(System.in);
        boolean exit = false;

        while (!exit) {
            System.out.println("\n===== 学生成绩管理系统 =====");
            System.out.println("1. 录入学生信息");
            System.out.println("2. 录入课程信息");
            System.out.println("3. 添加成绩");
            System.out.println("4. 查询成绩");
            System.out.println("5. 计算GPA");
            System.out.println("6. 保存信息到文件");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作（输入序号）：");

            int choice = scanner.nextInt();
            scanner.nextLine();

            try {
                switch (choice) {
                    case 1:
                        System.out.print("请输入学号：");
                        String stuId = scanner.nextLine();
                        System.out.print("请输入姓名：");
                        String name = scanner.nextLine();
                        System.out.print("请输入班级：");
                        String clazz = scanner.nextLine();
                        system.addStudent(new Student(stuId, name, clazz));
                        System.out.println("学生信息录入成功！");
                        break;

                    case 2:
                        System.out.print("请输入课程编号：");
                        String courseId = scanner.nextLine();
                        System.out.print("请输入课程名称：");
                        String courseName = scanner.nextLine();
                        System.out.print("请输入学分：");
                        int credit = scanner.nextInt();
                        scanner.nextLine();
                        system.addCourse(new Course(courseId, courseName, credit));
                        System.out.println("课程信息录入成功！");
                        break;

                    case 3:
                        System.out.print("请输入学生学号：");
                        String scoreStuId = scanner.nextLine();
                        System.out.print("请输入课程编号：");
                        String scoreCourseId = scanner.nextLine();
                        System.out.print("请输入分数（0-100）：");
                        double score = scanner.nextDouble();
                        scanner.nextLine();
                        system.addScore(scoreStuId, scoreCourseId, score);
                        System.out.println("成绩添加成功！");
                        break;

                    case 4:
                        System.out.print("请输入学生学号：");
                        String queryStuId = scanner.nextLine();
                        System.out.print("请输入课程编号：");
                        String queryCourseId = scanner.nextLine();
                        double result = system.queryScore(queryStuId, queryCourseId);
                        System.out.println("查询结果：" + result + "分");
                        break;

                    case 5:
                        System.out.print("请输入学生学号：");
                        String gpaStuId = scanner.nextLine();
                        double gpa = system.calculateGPA(gpaStuId);
                        System.out.println("该学生的GPA（4.0满）：" + gpa);
                        break;

                    case 6:
                        system.saveToFile("student_score_info.txt");
                        System.out.println("所有信息已保存到文件！");
                        break;

                    case 0:
                        exit = true;
                        System.out.println("感谢使用，再见！");
                        break;

                    default:
                        System.out.println("无效选择，请重新输入！");
                }
            } catch (Exception e) {
                System.out.println("操作异常：" + e.getMessage());
            }
        }

        scanner.close();
    }
}